package com.cwd.arm.inter.util;

import com.cwd.arm.application.dto.*;
import com.cwd.arm.application.dto.base.BaseDto;
import com.cwd.arm.application.dto.base.ProjectBaseDto;
import com.cwd.arm.application.dto.board.*;
import com.cwd.arm.application.dto.confirmIncome.ConfirmIncomeMonthAmountDto;
import com.cwd.arm.application.dto.confirmIncome.ProjectConfirmIncomeDto;
import com.cwd.arm.application.dto.project.*;
import com.cwd.arm.application.dto.user.UserManageDto;
import com.cwd.arm.common.dto.session.AuthDto;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.common.enums.*;
import com.cwd.arm.common.util.DateUtil;
import com.cwd.arm.inter.vo.BaseVo;
import com.cwd.arm.inter.vo.ProjectBaseVo;
import com.cwd.arm.inter.vo.auth.AuthVo;
import com.cwd.arm.inter.vo.board.*;
import com.cwd.arm.inter.vo.cashFlow.CashFlowMonthVo;
import com.cwd.arm.inter.vo.cashFlow.CashFlowTotalVo;
import com.cwd.arm.inter.vo.cashFlow.ProjectCashFlowMonthVo;
import com.cwd.arm.inter.vo.cashFlow.ProjectCashFlowVo;
import com.cwd.arm.inter.vo.confirmIncome.ConfirmIncomeMonthAmountVo;
import com.cwd.arm.inter.vo.confirmIncome.ConfirmIncomeVo;
import com.cwd.arm.inter.vo.confirmIncome.ProjectConfirmIncomeVo;
import com.cwd.arm.inter.vo.pay.PayVo;
import com.cwd.arm.inter.vo.project.ProjectReceiveDirectorVo;
import com.cwd.arm.inter.vo.project.ProjectDetailVo;
import com.cwd.arm.inter.vo.project.ProjectVo;
import com.cwd.arm.inter.vo.project.ReceiveDirectorVo;
import com.cwd.arm.inter.vo.receivableAmountVo.ProjectReceivableAmountTotalVo;
import com.cwd.arm.inter.vo.receivableAmountVo.ProjectReceivableAmountVo;
import com.cwd.arm.inter.vo.receivableAmountVo.ProjectReceivePlanVo;
import com.cwd.arm.inter.vo.receiveNode.ReceiveNodeVo;
import com.cwd.arm.inter.vo.receiveRecord.ReceiveRecordVo;
import com.cwd.arm.inter.vo.role.RoleVo;
import com.cwd.arm.inter.vo.tax.TaxVo;
import com.cwd.arm.inter.vo.user.UserManageVo;
import com.cwd.arm.inter.vo.user.UserVo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yan on 21-11-28.
 */
public class DtoToVoUtil {

    //权限----------------------->
    public static UserVo convertToUserVo(UserDto dto) {
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(dto, vo);
        List<AuthDto> authDtoList = dto.getAuthDtoList();

        List<AuthDto> btnAuthDtoList = authDtoList.stream()
                .filter(authDto -> authDto.getAuthType().equals(AuthTypeEnum.BTN.getCode()))
                .collect(Collectors.toList());
        List<AuthVo> btnAuthVoList = convertToAuthVoList(btnAuthDtoList);
        vo.setBtnAuthList(btnAuthVoList);

        List<AuthDto> menuAuthDtoList = authDtoList.stream()
                .filter(authDto -> authDto.getAuthType().equals(AuthTypeEnum.MENU.getCode()))
                .collect(Collectors.toList());
        List<AuthVo> menuAuthVoList = convertToAuthVoList(menuAuthDtoList);

        vo.setMenuAuthList(menuAuthVoList);
        return vo;
    }

    public static List<AuthVo> convertToAuthVoList(List<AuthDto> dtoList) {
        return dtoList.stream().map(btnAuthDto -> (convertToAuthVo(btnAuthDto))).collect(Collectors.toList());
    }

    public static AuthVo convertToAuthVo(AuthDto dto) {
        AuthVo vo = new AuthVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    //项目------------------->
    public static ProjectVo convertToVo(ProjectDto dto) {
        ProjectVo vo = new ProjectVo();
        BeanUtils.copyProperties(dto, vo);
        setProjectInfo(dto, vo);
        setBase(dto, vo);
        return vo;
    }


    public static List<ProjectVo> convertToProjectVoList(List<ProjectDto> dtoList) {
        return dtoList.stream().map(dto -> (convertToVo(dto))).collect(Collectors.toList());
    }

    public static ProjectDetailVo convertToDetailVo(ProjectDetailDto detailDto) {
        ProjectDetailVo detailVo = new ProjectDetailVo();

        ProjectDto projectDto = detailDto.getProject();
        ProjectVo projectVo = DtoToVoUtil.convertToVo(projectDto);
        detailVo.setProjectVo(projectVo);

        List<ReceiveNodeDto> nodeDtoList = detailDto.getNodeList();
        List<ReceiveNodeVo> nodeVoList = DtoToVoUtil.convertToReceiveNodeVoList(nodeDtoList);
        detailVo.setNodeVoList(nodeVoList);

        List<ConfirmIncomeDto> confirmIncomeDtoList = detailDto.getConfirmIncomeList();
        List<ConfirmIncomeVo> confirmIncomeDtoListVoList = DtoToVoUtil.convertToConfirmIncomeVoList(confirmIncomeDtoList);
        detailVo.setConfirmIncomeVoList(confirmIncomeDtoListVoList);

        List<ReceiveRecordDto> receiveRecordDtoList = detailDto.getReceiveRecordList();
        List<ReceiveRecordVo> receiveRecordDtoListVoList = DtoToVoUtil.convertToReceiveRecordVoList(receiveRecordDtoList);
        detailVo.setReceiveRecordVoList(receiveRecordDtoListVoList);

        List<PayDto> payDtoList = detailDto.getPayList();
        List<PayVo> payVoList = payDtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
        detailVo.setPayVoList(payVoList);

        List<TaxDto> taxDtoList = detailDto.getTaxDtoList();
        List<TaxVo> taxVoList = DtoToVoUtil.convertToTaxVoList(taxDtoList);
        detailVo.setTaxVoList(taxVoList);

        List<ProjectReceiveDirectorDto> projectReceiveDirectorDtoList = detailDto.getProjectReceiveDirectorDtoList();
        List<ProjectReceiveDirectorVo> projectReceiveDirectorVoList = DtoToVoUtil.convertToProjectReceiveDirectorVoList(projectReceiveDirectorDtoList);
        detailVo.setProjectReceiveDirectorVoList(projectReceiveDirectorVoList);
        return detailVo;
    }

    public static ReceiveNodeVo convertToVo(ReceiveNodeDto dto) {
        ReceiveNodeVo vo = new ReceiveNodeVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        vo.setCompletionRate(RateUtil.convertToRate(dto.getCompletionRate()));
//        vo.setType(NodeTypeEnum.getFileName(dto.getType()));
        vo.setUserName(dto.getShowName());
        vo.setEndDate(DateUtil.dateToDateStr(dto.getEndDate()));
        return vo;
    }

    public static List<ReceiveNodeVo> convertToReceiveNodeVoList(List<ReceiveNodeDto> dtoList) {
        return dtoList.stream().map(dto -> (convertToVo(dto))).collect(Collectors.toList());
    }

    public static ConfirmIncomeVo convertToVo(ConfirmIncomeDto dto) {
        ConfirmIncomeVo vo = new ConfirmIncomeVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        vo.setCompletionRate(RateUtil.convertToRate(dto.getCompletionRate()));
        vo.setConfirmDate(DateUtil.dateToDateStr(dto.getConfirmDate()));
        return vo;
    }

    public static List<ConfirmIncomeVo> convertToConfirmIncomeVoList(List<ConfirmIncomeDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ReceiveRecordVo convertToVo(ReceiveRecordDto dto) {
        ReceiveRecordVo vo = new ReceiveRecordVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
//        vo.setHonorPayType(HonorPayTypeEnum.getFileName(dto.getHonorPayType()));
        vo.setPayType(PayTypeEnum.getName(dto.getPayType()));
        vo.setHonorTaxRate(RateUtil.convertToRate(dto.getHonorTaxRate()));
        vo.setUserName(dto.getShowName());
        vo.setReceiveDate(DateUtil.dateToDateStr(dto.getReceiveDate()));
        vo.setHonorEndDate(DateUtil.dateToDateStr(dto.getHonorEndDate()));
        return vo;
    }

    public static List<ReceiveRecordVo> convertToReceiveRecordVoList(List<ReceiveRecordDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static PayVo convertToVo(PayDto dto) {
        PayVo vo = new PayVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        vo.setPayDate(DateUtil.dateToDateStr(dto.getPayDate()));
        return vo;
    }

    public static List<PayVo> convertToPayVoList(List<PayDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static TaxVo convertToVo(TaxDto dto) {
        TaxVo vo = new TaxVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        vo.setTaxDate(DateUtil.dateToDateStr(dto.getTaxDate()));
        return vo;
    }

    public static List<TaxVo> convertToTaxVoList(List<TaxDto> dtoList) {
        return dtoList.stream().map(dto -> (convertToVo(dto))).collect(Collectors.toList());
    }

    public static ProjectReceiveDirectorVo convertToVo(ProjectReceiveDirectorDto dto) {
        ProjectReceiveDirectorVo vo = new ProjectReceiveDirectorVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<ProjectReceiveDirectorVo> convertToProjectReceiveDirectorVoList(List<ProjectReceiveDirectorDto> dtoList) {
        return dtoList.stream().map(dto -> (convertToVo(dto))).collect(Collectors.toList());
    }

    public static ReceiveDirectorVo convertToVo(UserDto dto) {
        ReceiveDirectorVo vo = new ReceiveDirectorVo();
        if (dto.getRealName() == null) {
            vo.setReceiveDirectorName(dto.getUsername());
        } else {
            vo.setReceiveDirectorName(dto.getRealName());
        }
        vo.setReceiveDirectorId(dto.getId());
        return vo;
    }

    public static List<ReceiveDirectorVo> convertToReceiveDirectorVoList(List<UserDto> dtoList) {
        return dtoList.stream().map(dto -> (convertToVo(dto))).collect(Collectors.toList());
    }

    //收款--------------------------->
    public static ProjectReceivableAmountVo convertToVo(ProjectReceivableAmountDto dto) {
        ProjectReceivableAmountVo vo = new ProjectReceivableAmountVo();
        BeanUtils.copyProperties(dto, vo);
        setProjectInfo(dto, vo);
        setBase(dto, vo);
        vo.setNextWeekEndDate(DateUtil.dateToDateStr(dto.getNextWeekEndDate()));
        return vo;
    }

    public static List<ProjectReceivableAmountVo> convertToProjectReceivableAmountVoList(List<ProjectReceivableAmountDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ProjectReceivableAmountTotalVo convertToVo(ProjectReceivableAmountTotalDto dto) {
        ProjectReceivableAmountTotalVo vo = new ProjectReceivableAmountTotalVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<ProjectReceivePlanVo> convertToProjectReceivePlanVoList(List<ProjectReceivePlanDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ProjectReceivePlanVo convertToVo(ProjectReceivePlanDto dto) {
        ProjectReceivePlanVo vo = new ProjectReceivePlanVo();
        BeanUtils.copyProperties(dto, vo);
        setProjectInfo(dto, vo);
        setBase(dto, vo);
        return vo;
    }

    //确认收入--------------------------------->
    public static List<ProjectConfirmIncomeVo> convertTPorojectConfirmIncomeVoList(List<ProjectConfirmIncomeDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }


    public static ProjectConfirmIncomeVo convertToVo(ProjectConfirmIncomeDto dto) {
        ProjectConfirmIncomeVo vo = new ProjectConfirmIncomeVo();
        BeanUtils.copyProperties(dto, vo);
        setProjectInfo(dto, vo);
        setBase(dto, vo);
        vo.setRtRate(RateUtil.convertToRate(dto.getRtRate()));
        vo.setFyTargetRate(RateUtil.convertToRate(dto.getFyTargetRate()));
        vo.setMonthAmountVoList(convertToConfirmIncomeMonthAmountVoList(dto.getMonthAmountDtoList()));
        return vo;
    }

    public static List<ConfirmIncomeMonthAmountVo> convertToConfirmIncomeMonthAmountVoList(List<ConfirmIncomeMonthAmountDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ConfirmIncomeMonthAmountVo convertToVo(ConfirmIncomeMonthAmountDto dto) {
        ConfirmIncomeMonthAmountVo vo = new ConfirmIncomeMonthAmountVo();
        BeanUtils.copyProperties(dto, vo);
        vo.setRate(RateUtil.convertToRate(dto.getRate()));
        return vo;
    }

    //现金流-------------------------->
    public static List<ProjectCashFlowVo> convertToProjectCashFlowVoList(List<ProjectCashFlowDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ProjectCashFlowVo convertToVo(ProjectCashFlowDto dto) {
        ProjectCashFlowVo vo = new ProjectCashFlowVo();
        BeanUtils.copyProperties(dto, vo);
        setProjectInfo(dto, vo);
        vo.setCashFlowMonthVoList(convertToProjectCashFlowMonthVoList(dto.getCashFlowMonthDtoList()));
        return vo;
    }

    public static List<ProjectCashFlowMonthVo> convertToProjectCashFlowMonthVoList(List<ProjectCashFlowMonthDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static ProjectCashFlowMonthVo convertToVo(ProjectCashFlowMonthDto dto) {
        ProjectCashFlowMonthVo vo = new ProjectCashFlowMonthVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static CashFlowTotalVo convertToVo(CashFlowTotalDto dto) {
        CashFlowTotalVo vo = new CashFlowTotalVo();
        BeanUtils.copyProperties(dto, vo);
        vo.setCashFlowMonthVoList(convertToCashFlowTotalVoList(dto.getCashFlowMonthDtoList()));
        return vo;
    }

    public static List<CashFlowMonthVo> convertToCashFlowTotalVoList(List<CashFlowMonthDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static CashFlowMonthVo convertToVo(CashFlowMonthDto dto) {
        CashFlowMonthVo vo = new CashFlowMonthVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    //管理用户------------------------->
    public static List<UserManageVo> convertToUserManageVoList(List<UserManageDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static UserManageVo convertToVo(UserManageDto dto) {
        UserManageVo vo = new UserManageVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        return vo;
    }

    //角色--------------------->
    public static List<RoleVo> convertToRoleVoList(List<RoleDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    public static RoleVo convertToVo(RoleDto dto) {
        RoleVo vo = new RoleVo();
        BeanUtils.copyProperties(dto, vo);
        setBase(dto, vo);
        return vo;
    }

    //看板-------------------------------->
    public static MsgVo convertToVo(MsgDto dto) {
        MsgVo vo = new MsgVo();
        BeanUtils.copyProperties(dto, vo);
        vo.setCreateTime(DateUtil.dateToDateStr(dto.getCreateTime()));
        return vo;
    }

    public static List<MsgVo> convertToMsgVoList(List<MsgDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }

    //看板-------------------------->
    public static RtVo convertToVo(RtDto dto) {
        RtVo vo = new RtVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static MonthAmountVo convertToVo(MonthAmountDto dto) {
        MonthAmountVo vo = new MonthAmountVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<MonthAmountVo> convertToMonthAmountVoList(List<MonthAmountDto> dtoList) {
        List<MonthAmountVo> amountVoList = new ArrayList<>(12);
        for (int i = 1; i < 13; i++) {
            int month = i;
            MonthAmountDto monthAmountDto = dtoList.stream().filter(dto -> dto.getMonth().equals(month)).findAny().orElse(null);
            MonthAmountVo monthAmountVo;
            if (monthAmountDto != null) {
                monthAmountVo = convertToVo(monthAmountDto);
            } else {
                monthAmountVo = new MonthAmountVo();
                monthAmountVo.setMonth(i);
            }
            amountVoList.add(monthAmountVo);
        }
        return amountVoList;
    }

    public static MonthRateVo convertToVo(MonthRateDto dto) {
        MonthRateVo vo = new MonthRateVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<MonthRateVo> convertToMonthRateVoList(List<MonthRateDto> dtoList) {
        List<MonthRateVo> monthRateVoList = new ArrayList<>(12);
        for (int i = 1; i < 13; i++) {
            int month = i;
            MonthRateDto monthRateDto = dtoList.stream().filter(dto -> dto.getMonth().equals(month)).findAny().orElse(null);
            MonthRateVo monthRateVo;
            if (monthRateDto != null) {
                monthRateVo = convertToVo(monthRateDto);
            } else {
                monthRateVo = new MonthRateVo();
                monthRateVo.setMonth(i);
            }
            monthRateVoList.add(monthRateVo);
        }
        return monthRateVoList;
    }

    public static QAmountVo convertToVo(QAmountDto dto) {
        QAmountVo vo = new QAmountVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<QAmountVo> convertToQAmountVoList(List<QAmountDto> dtoList) {
        List<QAmountVo> qAmountVoList = new ArrayList<>(4);
        for (int i = 1; i < 5; i++) {
            int q = i;
            QAmountDto qAmountDto = dtoList.stream().filter(dto -> dto.getQ().equals(q)).findAny().orElse(null);
            QAmountVo qAmountVo;
            if (qAmountDto != null) {
                qAmountVo = convertToVo(qAmountDto);
            } else {
                qAmountVo = new QAmountVo();
                qAmountVo.setQ(i);
            }
            qAmountVoList.add(qAmountVo);
        }
        return qAmountVoList;
    }

    public static YearAmountVo convertToVo(YearAmountDto dto) {
        YearAmountVo vo = new YearAmountVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    public static List<YearAmountVo> convertToYearAmountVoList(List<YearAmountDto> dtoList) {
        return dtoList.stream().map(dto -> DtoToVoUtil.convertToVo(dto)).collect(Collectors.toList());
    }


    private static void setProjectInfo(ProjectBaseDto dto, ProjectBaseVo vo) {
        vo.setUserName(dto.getShowName());
        vo.setProjectType(dto.getProjectTypeKey());
//        vo.setProjectType(ProjectTypeEnum.getFileName(dto.getProjectType()));
        vo.setProjectStatus(dto.getProjectStatus().toString());
//        vo.setBusinessType(BusinessTypeEnum.getFileName(dto.getBusinessType()));
        vo.setContractRate(RateUtil.convertToRate(dto.getContractRate()));
        vo.setStartDate(DateUtil.dateToDateStr(dto.getStartDate()));
        vo.setEndDate(DateUtil.dateToDateStr(dto.getEndDate()));
        vo.setContractDate(DateUtil.dateToDateStr(dto.getContractDate()));
        vo.setTaxPoint(RateUtil.convertToRate(dto.getTaxPoint()));
        vo.setConfirmContractRate(RateUtil.convertToRate(dto.getConfirmContractRate()));
        vo.setCloseDate(DateUtil.dateToDateStr(dto.getCloseDate()));
    }

    private static void setBase(BaseDto dto, BaseVo vo) {
        vo.setUserName(dto.getShowName());
        vo.setCreateTime(DateUtil.dateToTimeStr(dto.getCreateTime()));
        vo.setUpdateTime(DateUtil.dateToTimeStr(dto.getUpdateTime()));
    }


}
